package com.bootdo.lvyou.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bootdo.common.utils.*;
import com.bootdo.common.wxpay.CommonUtil;
import com.bootdo.common.wxpay.MoneyUtils;
import com.bootdo.common.wxpay.TuikuanUtils;
import com.bootdo.common.wxpay.WxPayHelper;
import com.bootdo.lvyou.domain.XcxConfig;
import com.bootdo.lvyou.service.XcxConfigService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ModelAttribute;
import com.bootdo.common.controller.BaseController;
import com.bootdo.lvyou.domain.LvyouScenicTicketOrder;
import com.bootdo.lvyou.service.LvyouScenicTicketOrderService;

/**
 * 
 * 
 * @author haojiangbo
 * @email 2100272728@qq.com
 * @date 2019-10-14 14:54:26
 */
 
@Controller
@RequestMapping("/lvyou/lvyouScenicTicketOrder")
public class LvyouScenicTicketOrderController extends BaseController{


	@Autowired
	private LvyouScenicTicketOrderService lvyouScenicTicketOrderService;

	@Autowired
	private XcxConfigService xcxConfigService;

	@ModelAttribute
	public LvyouScenicTicketOrder get(@RequestParam(required=false) Long id) {
		if (id != null){
			return lvyouScenicTicketOrderService.get(id);
		}else{
			return new LvyouScenicTicketOrder();
		}
	}


	
	//insert into sys_menu(parent_id,name,url,perms,type)  values (149,'测试','/lvyou/lvyouScenicTicketOrder','lvyou:lvyouScenicTicketOrder:lvyouScenicTicketOrder',1)
	//insert into sys_menu(parent_id,name,url,perms,type)  values (160,'修改','','lvyou:lvyouScenicTicketOrder:edit',2)
	//type 0是分类菜单 1是小菜但 2是按钮
	@GetMapping()
	@RequiresPermissions("lvyou:lvyouScenicTicketOrder:lvyouScenicTicketOrder")	
	String LvyouScenicTicketOrder(LvyouScenicTicketOrder lvyouScenicTicketOrder,HttpServletRequest request,HttpServletResponse response){
	    return "lvyou/lvyouScenicTicketOrder/lvyouScenicTicketOrder";
	}
	
	@ResponseBody
	@PostMapping("/list")
	@RequiresPermissions("lvyou:lvyouScenicTicketOrder:lvyouScenicTicketOrder")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		if(!isAdmin()){
			query.put("userId", getUserId());
		}
		List<LvyouScenicTicketOrder> lvyouScenicTicketOrderList = lvyouScenicTicketOrderService.list(query);
		int total = lvyouScenicTicketOrderService.count(query);
		PageUtils pageUtils = new PageUtils(lvyouScenicTicketOrderList, total);
		return pageUtils;
	}



	/**
	 *  /lvyou/lvyouScenicTicketOrder/create_tk_order
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/create_tk_order")
	public R createTkOrder(
			@RequestParam("orderNo") String orderNo,
			HttpServletRequest request,HttpServletResponse response){
		LvyouScenicTicketOrder lvyouScenicTicketOrder = lvyouScenicTicketOrderService.getByOrderNoAndStatus(orderNo, null);
		if(null == lvyouScenicTicketOrder){
			return R.error("该订单不允许退款");
		}
		try {

			if(lvyouScenicTicketOrder.getStatus().intValue() == 1){
				return R.error("该订单还未支付");
			}

			if(lvyouScenicTicketOrder.getStatus().intValue() == 5){
				return R.error("订单已核销,不能退款");
			}

			if(lvyouScenicTicketOrder.getStatus().intValue() == 2){
				lvyouScenicTicketOrder.setStatus(3);
			}
			String r =  createTuikuanPar(lvyouScenicTicketOrder,request);
			Hog.e("退款参=={}",r);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error(e.getMessage());
		}
		lvyouScenicTicketOrderService.save(lvyouScenicTicketOrder);
		return R.ok("您的退款已提交申请,请耐心等待");
	}


	/**
	 * 创建退款js
	 * @param order
	 * @return
	 * @throws Exception
	 */
	String createTuikuanPar(LvyouScenicTicketOrder order,HttpServletRequest request) throws Exception{
		XcxConfig xcxConfig =  xcxConfigService.getDefalt();
		String nonce_str = CommonUtil.CreateNoncestr();
		String basePath = request.getScheme() + "://"
				+ request.getServerName() + request.getContextPath();
		WxPayHelper wxPayHelper = new WxPayHelper();
		Map map = new HashMap();
		map.put("appid", xcxConfig.getAppId());
		map.put("mch_id", xcxConfig.getMemchantNo());
		map.put("nonce_str", nonce_str);
		map.put("notify_url", basePath
				+ "/f/scenicspot/order/v1/wxTuikuanCallBack");
		map.put("transaction_id",order.getPayOrderNo());
		map.put("out_refund_no", SnowflakeIdWorker.getOrderNo("TK"));
		map.put("total_fee", String.format("%.0f",order.getTotalMoney()*100));
		map.put("refund_fee", String.format("%.0f",order.getTotalMoney()*100));
		map.put("sign", MoneyUtils.createSign(map, xcxConfig.getApiKey()));// 签名
		return TuikuanUtils.createTuikuan(map, xcxConfig.getMemchantNo(), xcxConfig.getBookPath());
	}



	@GetMapping("/add")
	@RequiresPermissions("lvyou:lvyouScenicTicketOrder:edit")
	String add(LvyouScenicTicketOrder lvyouScenicTicketOrder,HttpServletRequest request,HttpServletResponse response){
	    return "lvyou/lvyouScenicTicketOrder/lvyouScenicTicketOrderedit";
	}

	@PostMapping("/getById")
	@ResponseBody
	R getById(LvyouScenicTicketOrder lvyouScenicTicketOrder) {
		return R.ok().put("i",lvyouScenicTicketOrder);
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("lvyou:lvyouScenicTicketOrder:edit")
	public R save( LvyouScenicTicketOrder lvyouScenicTicketOrder){
	
		if(lvyouScenicTicketOrderService.save(lvyouScenicTicketOrder)>0){
			return R.ok();
		}
		return R.error();
	}
	
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("lvyou:lvyouScenicTicketOrder:edit")
	public R remove( Long id){
		if(lvyouScenicTicketOrderService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("lvyou:lvyouScenicTicketOrder:edit")
	public R remove(@RequestParam("ids[]") Long[] ids){
		lvyouScenicTicketOrderService.batchRemove(ids);
		return R.ok();
	}
	
}